﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ExpressionTreeLib.ViewModels
{
    [ExpressionType(typeof(ParameterExpression))]
    public class ParameterViewModel : NodeViewModel
    {
        private string _parameterName;
        public string ParameterName
        {
            get { return _parameterName; }
            set 
            {
                _parameterName = value;
                this.NotifyOfPropertyChange(()=>ParameterName);
            }
        }

        private Type _parameterType;

        [TypeConverter(typeof(TypeTypeConverter))]
        public Type ParameterType
        {
            get { return _parameterType; }
            private set 
            {
                _parameterType = value;
                this.NotifyOfPropertyChange(()=>ParameterType); 
            }
        }

        public override void LoadExpressionNode(Expression expression)
        {
            base.LoadExpressionNode(expression);
            
            var pe = expression as ParameterExpression;


            this._parameterName = pe.Name;
            this._parameterType = pe.Type;
        }

        protected override Expression CreateExpression()
        {
            return Expression.Parameter(this.ParameterType, this.ParameterName);
        }

        public override void GenerateCode(StringBuilder stringBuilder)
        {
            stringBuilder.AppendFormat("Expression.Parameter(typeof({0}), \"{1}\")", this.ParameterType.Name,
                                       this.ParameterName);
        }
    }
}
